package app.ui;

import java.util.ArrayList;

import org.apache.http.client.CircularRedirectException;

import com.facebook.android.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import database.local.Business;
import database.local.Deal;
import database.local.PicturesUtil;
import database.local.UserImageLink;

public class DealsListView extends Activity {
	Business currentBusiness;
	TextView nameText, addressText, websiteText;
	ImageView imageImage;
	ListView dealsListView;
	Button toMapButton;
	GridView usersGridView;
	ImageAdapter userImageAdapter;
	BusinessViewSyncronizer dbc;
	private BitmapSyncronizer dbs;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deal_list_layout);
        
        //get deal and business from bundle.
	    Bundle b = getIntent().getBundleExtra("values");
	    currentBusiness = b.getParcelable("business");
	    
	    //inflate fields
	    inflateFields();
	    
	    //populate fields
	    populateFields();
	    
	    //populate grid
	    userImageAdapter = new ImageAdapter(this);
	    
	    usersGridView.setAdapter(userImageAdapter);
	    
	    usersGridView.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	            String link = ((UserImageLink) userImageAdapter.getItem(position)).getLink();
	            final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(link));
	            startActivity(intent);
	        }
	    });

	    
	    //populate list
	    dealsListView.setAdapter(new DealsListArrayAdapter(this, currentBusiness.getDeals(), currentBusiness));
	    dealsListView.setOnItemClickListener( new AdapterView.OnItemClickListener() {

			
	    	public void onItemClick(AdapterView<?> parentView, View childView, int position,
					long id) {
				openDealWindow(currentBusiness, currentBusiness.getDeals().get(position));
			}
		});
	}


	private void populateFields() {
		nameText.setText(currentBusiness.getName());
		addressText.setText(currentBusiness.getFullAddress());
		
		if (currentBusiness.getWebsite().equalsIgnoreCase(""))
			websiteText.setText("No website available" );
		else
			websiteText.setText(currentBusiness.getWebsite());
		
		if (!currentBusiness.getPic_link().equalsIgnoreCase(""))
		{
			dbs = new BitmapSyncronizer(this);
			dbs.getBitmap(currentBusiness.getPic_link(), true, currentBusiness);
		}
		else
		{
			imageImage.setImageDrawable(PicturesUtil.getBusinessPicBig(currentBusiness, this));
		}
		
		// toMap button
//		toMapButton.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View view) {				
//			Intent toMapIntent = new Intent(DealsListView.this, AndroAppActivity.class);
//			toMapIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//			
//			Bundle bundle = new Bundle();			
//			bundle.putParcelable("business", currentBusiness);			
//
//			toMapIntent.putExtra("values", bundle);
//						
//			DealsListView.this.startActivity(toMapIntent);		
//			}
//		});
//		
		dbc = new BusinessViewSyncronizer(this);
		dbc.sync(currentBusiness);
		
	}


	private void inflateFields() {
		//TextView
		nameText = (TextView) findViewById(R.id.dealist_business_name_text);
		addressText = (TextView) findViewById(R.id.dealist_address_text);
		websiteText = (TextView) findViewById(R.id.dealist_website_text);
		
		//ImageView
		imageImage = (ImageView) findViewById(R.id.dealist_business_image);
		
		//ListView
		dealsListView = (ListView) findViewById(R.id.dealist_list_listview);
		
		//GridView
		usersGridView = (GridView) findViewById(R.id.dealist_usersImage_grid);
		
		// toMap button
//		toMapButton = (Button) findViewById(R.id.dealist_to_map_button);
	}
	
	protected void openDealWindow(Business b, Deal d) {
		Intent openDealActivity = new Intent(this, DealViewActivity.class);
		Bundle bundle = new Bundle();
		
		bundle.putParcelable("deal", d);
		bundle.putParcelable("business", b);
		
		openDealActivity.putExtra("values", bundle);
		this.startActivity(openDealActivity);
		}
	
	


	public class ImageAdapter extends BaseAdapter {
	    private Context mContext;
	    private ArrayList<UserImageLink> userImageList;
	    
	    public ImageAdapter(Context c) {
	    	userImageList = new ArrayList<UserImageLink>();
	        mContext = c;
	    }

	    public int getCount() {
	        return userImageList.size();
	    }

	    public Object getItem(int position) {
	        return userImageList.get(position);
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView imageView;
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	            imageView = new ImageView(mContext);
	            imageView.setLayoutParams(new GridView.LayoutParams(60,70));
	            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	            imageView.setPadding(10, 0, 10, 0);
	        } else {
	            imageView = (ImageView) convertView;
	        }

	        imageView.setImageBitmap(userImageList.get(position).getImage());
	        return imageView;
	    }

	    public void addUserImageLink(UserImageLink uil){
	    	userImageList.add(uil);
	    	notifyDataSetChanged();
	    }

	}
	
	public void addImageToGrid(Bitmap image, String link){
		if (userImageAdapter != null)
			userImageAdapter.addUserImageLink(new UserImageLink(image, link));
				
	}
	
	
	
}
